package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZLJ
 * @date 2025/10/9 10:53
 * @Description
 */
@Slf4j
@RestController
@SuppressWarnings("all")
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        log.info("删除课程id:{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject){
        courseService.getBySubject(subject);
        log.info("查询课程学科:{}",subject);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectAll(){
        List<Course> list = courseService.selectAll();
        log.info("查询所有课程");
        return Result.success(list);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        log.info("修改课程:{}",course);
        courseService.update(course);
        return Result.success();
    }

    @PostMapping
    public Result addCourse(@RequestBody Course course){
        log.info("新增课程:{}",course);
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Course course = courseService.getById(id);
        log.info("查询课程id:{}",id);
        return Result.success(course);
    }

    @GetMapping
    public Result page(@RequestParam (name = "name",required = false) String name,
                       @RequestParam (name = "subject",required = false) Integer subject,
                       @RequestParam (name = "target",required = false) String target,
                       @RequestParam (name = "page",required = true,defaultValue = "1") Integer page,
                       @RequestParam (name = "pageSize",required = true,defaultValue = "10") Integer pageSize){
        PageResult<Course> pageResult =courseService.page(name,subject,target,page,pageSize);
        log.info("分页查询课程,参数:课程名称={},课程学科={},适用人群={},当前页码={},每页大小={}", name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }
}
